package com.zhulanhua.blogs.controller;

import com.zhulanhua.blogs.Utils.Code;
import com.zhulanhua.blogs.Utils.DozerUtils;
import com.zhulanhua.blogs.Utils.Result;
import com.zhulanhua.blogs.controller.params.FriendLinkParams;
import com.zhulanhua.blogs.domain.FriendLink;
import com.zhulanhua.blogs.service.FriendLinkSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 友链接口
 * */
@RestController
@RequestMapping("/api/friends")
@CrossOrigin
public class FriendLinkController {

    @Autowired
    FriendLinkSerice friendLinkSerice;
    @Autowired
    DozerUtils dozerUtils;


    /**
     * 获取审核不通过的友链列表
     * @param page  第几页，默认为1
     * @param limit 一页多少个，默认为10
     * @param type  友链类型，0为等待审核，1审核通过，2审核不通过，默认为1
     * */
    @GetMapping("/list")
    public Result<List<FriendLink>> getErr(@RequestParam(value = "page",defaultValue = "1") int page,
                                           @RequestParam(value = "limit",defaultValue = "10") int limit,
                                           @RequestParam(value = "type",defaultValue = "1") int type){
        return new Result(Code.OK, friendLinkSerice.getNO(type));
    }

    /**
     * 上传友链
     * @param friendLink
     * */
    @PostMapping("/save")
    public Result save(@RequestBody FriendLinkParams friendLink){
        friendLinkSerice.save(dozerUtils.convert(friendLink,FriendLink.class));
        return Result.ok();
    }

    /**
     * 删除友链
     * @param id 友链id
     * */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam int id){
        friendLinkSerice.delete(id);
        return Result.ok();
    }

    /**
     * 友链审核通过
     * @param id 友链id
     * */
    @PutMapping("/ok")
    public Result updateOk(@RequestParam int id){
        friendLinkSerice.update(id, 1);
        return Result.ok();
    }

    /**
     * 友链审核不通过
     * @param id 友链id
     * */
    @PutMapping("/no")
    public Result updateNo(@RequestParam int id){
        friendLinkSerice.update(id, 1);
        return Result.ok();
    }
}
